package com.isoftstone.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.isoftstone.pojo.PaymentRecord;
import com.isoftstone.service.PaymentRecordService;
import com.isoftstone.util.Result;
import jakarta.validation.Valid;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api")
public class PaymentRecordController {

    private final PaymentRecordService paymentRecordService;

    @Autowired
    public PaymentRecordController(PaymentRecordService paymentRecordService) {
        this.paymentRecordService = paymentRecordService;
    }
    private static final Logger log = LoggerFactory.getLogger(StudentController.class);
    // 获取所有缴费记录
    @GetMapping("paymentRecord")
    public Result<?> getAllPaymentRecords(@RequestParam(defaultValue = "1") Integer currentPage,
                                    @RequestParam(defaultValue = "5") Integer pageSize){
        Page page = paymentRecordService.getAllPaymentRecords(currentPage, pageSize);
        PageInfo<Object> pageInfo = new PageInfo<>(page);
        return Result.success(pageInfo);
    }
    //根据缴费编号获取缴费记录
    @GetMapping("paymentRecord/{id}")
    public ResponseEntity<PaymentRecord> getPaymentRecordById(@PathVariable Integer id) {
        return paymentRecordService.getPaymentRecordById(id)
                .map(ResponseEntity::ok)
                .orElse(ResponseEntity.notFound().build());
    }
    //添加缴费记录
    @PostMapping("paymentRecord")
    @ResponseStatus(HttpStatus.CREATED)
    public Result<?> insertPaymentRecord(@RequestBody PaymentRecord paymentRecord) {
        return paymentRecordService.insertPaymentRecord(paymentRecord);
    }
    //修改缴费记录
    @PutMapping("paymentRecord/{id}")
    public ResponseEntity<Result<?>> updatePaymentRecord(
            @PathVariable Integer id,
            @RequestBody PaymentRecord paymentRecordDetails) {
        try {
            Result<?> updatedPaymentRecord = paymentRecordService.updatePaymentRecord(id, paymentRecordDetails);
            return ResponseEntity.ok(updatedPaymentRecord);
        } catch (RuntimeException e) {
            return ResponseEntity.notFound().build();
        }
    }
    //删除缴费记录
    @DeleteMapping("paymentRecord/{id}")
    public ResponseEntity<Void> deletePaymentRecord(@PathVariable Integer id) {
        paymentRecordService.deletePaymentRecord(id);
        return ResponseEntity.noContent().build();
    }
    //根据学生学号获取缴费记录
    @GetMapping("paymentRecord/studentNumber")
    public Result<?> getPaymentRecordsByStudentId(@RequestParam String studentNumber,
                                                  @RequestParam(defaultValue = "1") Integer currentPage,
                                                  @RequestParam(defaultValue = "5") Integer pageSize) {
        Page page = paymentRecordService.getPaymentRecordsByStudentId(studentNumber, currentPage, pageSize);
        PageInfo<Object> pageInfo = new PageInfo<>(page);
        return Result.success(pageInfo);
    }
    //根据缴费项目名称获取缴费记录
    @GetMapping("paymentRecord/payment")
    public Result<?> getPaymentRecordsByItemName(@RequestParam String ItemName,
                                                     @RequestParam(defaultValue = "1") Integer currentPage,
                                                     @RequestParam(defaultValue = "5") Integer pageSize) {
        Page page = paymentRecordService.getPaymentRecordsByItemName(ItemName, currentPage, pageSize);
        PageInfo<Object> pageInfo = new PageInfo<>(page);
        return Result.success(pageInfo);
    }
    //根据缴费状态获取缴费记录
    @GetMapping("paymentRecord/status")
    public Result<?> getPaymentRecordsByStatus(@RequestParam String status,
                                              @RequestParam(defaultValue = "1") Integer currentPage,
                                              @RequestParam(defaultValue = "5") Integer pageSize) {
        Page page = paymentRecordService.getPaymentRecordsByStatus(status, currentPage, pageSize);
        PageInfo<Object> pageInfo = new PageInfo<>(page);
        return Result.success(pageInfo);
    }
    //根据缴费方式获取缴费记录
    @GetMapping("paymentRecord/paymentMethod")
    public Result<?> getPaymentRecordsByPaymentMethod(@RequestParam String paymentMethod,
                                                     @RequestParam(defaultValue = "1") Integer currentPage,
                                                     @RequestParam(defaultValue = "3") Integer pageSize) {
        Page page = paymentRecordService.getPaymentRecordsByPaymentMethod(paymentMethod, currentPage, pageSize);
        PageInfo<Object> pageInfo = new PageInfo<>(page);
        return Result.success(pageInfo);
    }

    //校验学号和缴费项目是否已存在
    @GetMapping("paymentRecord/check")
    public boolean checkStudentNumberAndItemName(@RequestParam String studentNumber, @RequestParam String itemName){
        return paymentRecordService.checkPaymentRecord(studentNumber,itemName);
    }

    /**
     * 学生端点击【确认支付】后调用
     * @param dto 仅含学生学号 + 缴费项目名称 + 支付方式
     */
    @PostMapping("/paymentRecord/pay")
    public Result<?> pay(@RequestBody @Valid PaymentRecord dto) {
        return paymentRecordService.pay(dto);
    }
    /**
     * 一键支付全部待缴项目
     */
    @PostMapping("/paymentRecord/payAll")
    public Result<?> payAll(@RequestBody @Valid PaymentRecord dto) {
        return paymentRecordService.payAll(dto);
    }

    /**
     * 学生端-待缴列表（仅未支付）
     */
    @GetMapping("/paymentRecord/unpaid")
    public Result<List<PaymentRecord>> getUnpaidList(@RequestParam String studentNumber){
        List<PaymentRecord> list = paymentRecordService.getUnpaidList(studentNumber);
        return Result.success(list);
    }

}
